class_lis = ['class="TRS_Editor"', 'id="zoom"', 'id="Zoom"', 'class="txt"', 'class="zwcontent"',
             'class="main_con_zw"', 'class="Article"', 'class="ArticleContent"', 'id="zoomcon"', 'class="textbody"']
Filter_Words = [
    '此页面上的内容需要较新版本的',
    ' Adobe Flash Player',
    '网站申明',
    '使用帮助',
    '联系我们',
    '主办单位',
    '技术支持',
    '版权所有',
    '备案号',
    '公安备案号',
    '邮箱',
    'icp',
    'ICP',
    '关键字',
    '关键词',
    '网友评论',
    '登录',
    '注册',
    '最热评论',
    '评论',
    '首页',
    '推荐',
    '网站地图',
    '本文链接',
    '转载请注明文章出处',
]

lenfw = len(Filter_Words)
filter_words_scale = 0.2


def get_zwlist(resp):
    zwlist5 = get_zwlist_by_p_attr(resp)
    if zwlist5 and len(zwlist5) > 3:
        if len(''.join(zwlist5)) > 50:
            zwlist = zwlist5
            return zwlist
    zwlist1 = get_zwlist_by_span_attr(resp)
    zwlist2 = get_zwlist_by_p(resp)
    zwlist3 = get_zwlist_by_br(resp)
    zwlist4 = get_zwlist_by_table_p(resp)

    zwlists = [zwlist1, zwlist2, zwlist3, zwlist4, zwlist5]
    lenlist = [len(i) if i else 0 for i in zwlists]
    maxindex = lenlist.index(max(lenlist))
    zwlist = zwlists[maxindex]
    if zwlist:
        return zwlist
    else:
        return ''


def get_zwlist_by_p_attr(resp):
    zwlist = ''
    for cl in class_lis:
        zwlist = resp.xpath(f'//*[@{cl}]//p | //*[@{cl}]//blockquote').xpath('string(.)').extract()
        if not zwlist:
            continue
        else:
            break
    return zwlist


def get_zwlist_by_span_attr(resp):
    zwlist = ''
    for cl in class_lis:
        zwlist = resp.xpath('//*[@{}]//span'.format(cl)).xpath('string(.)').extract()
        if not zwlist:
            continue
        else:
            break
    return zwlist


def get_zwlist_by_p(resp):
    p_parents = resp.xpath('//*/p/..')
    if p_parents:
        dic = {}
        for did, div in enumerate(p_parents):
            p = div.xpath('./p | ./blockquote')
            p_num = len(p)
            word_num = sum([len(pp) for pp in p.xpath('string(.)').extract()])
            pcontent = div.xpath('string(.)').extract_first()
            if content_filter(pcontent):
                dic.setdefault(did, [p_num, word_num])
        if not dic:
            return ''
        llv = Grading_instrument(dic)
        zwlist = p_parents[llv].xpath('./p | ./blockquote').xpath('string(.)').extract()
        zwtemp = ''.join([zl.strip() for zl in zwlist])
        if not zwtemp:
            return ''
        if zwlist:
            return zwlist
        else:
            return ''
    else:
        return ''


def get_zwlist_by_br(resp):
    p_parents = resp.xpath('//*/br/..')
    if p_parents:
        dic = {}
        for did, div in enumerate(p_parents):
            p = div.xpath('./text()')
            p_num = len(p)
            word_num = sum([len(pp) for pp in p.extract()])
            pcontent = div.xpath('string(.)').extract_first()
            if content_filter(pcontent):
                dic.setdefault(did, [p_num, word_num])
        if not dic:
            return ''
        llv = Grading_instrument(dic)
        zwlist = p_parents[llv].xpath('./*/text() | ./text() ').extract()
        zwtemp = ''.join([zl.strip() for zl in zwlist])
        if not zwtemp:
            return ''

        if zwlist:
            return zwlist
        else:
            return ''
    else:
        return ''


def get_zwlist_by_table_p(resp):
    # / ancestor::table
    p_parents = resp.xpath('//table//p/ancestor::table')
    if p_parents:
        dic = {}
        for did, div in enumerate(p_parents):
            p = div.xpath('.//p')
            p_num = len(p)
            word_num = sum([len(pp) for pp in p.xpath('string(.)').extract()])
            pcontent = div.xpath('string(.)').extract_first()
            if content_filter(pcontent):
                dic.setdefault(did, [p_num, word_num])
        if dic:
            llv = Grading_instrument(dic)
            zwlist = p_parents[llv].xpath('string(.)').extract()
            zwtemp = ''.join([zl.strip() for zl in zwlist])
        else:
            return ''
        if not zwtemp:
            return ''

        if zwlist:
            return zwlist
        else:
            return ''
    else:
        return ''


def content_filter(pcontent):
    hnum = 0
    key_in_content = ''
    for fi in Filter_Words:
        if fi in pcontent:
            key_in_content += fi
            hnum += 1
    lenpcontentstrip = len(pcontent.strip())
    if not lenpcontentstrip:
        lenpcontentstrip = 1
    key_in_content_scale = len(key_in_content) / lenpcontentstrip
    if hnum / lenfw > filter_words_scale or key_in_content_scale > 0.1:
        return False
    else:
        return True


def Grading_instrument(dic):
    lis_id = list(dic.keys())
    lis_p_num = [i[0] for i in list(dic.values())]
    # lis_word_num = [i[1] for i in list(dic.values())]

    index = lis_p_num.index(max(lis_p_num))
    id = lis_id[index]
    return id


def get_person_by_zwlist(zwlist):
    dsr = [i for i in zwlist if i.strip().startswith('当事人')]
    person = ''
    if dsr:
        dsr = dsr[0].strip()
        a_ = len(dsr.split(',')[0])
        b_ = len(dsr.split('，')[0])
        if a_ < b_:
            person = dsr.replace('当事人', '')[1:].split(',')[0]
        else:
            person = dsr.replace('当事人', '')[1:].split('，')[0]
    else:
        for id, i in enumerate(zwlist):
            i = i.replace(' ', '')
            if 10 > len(i) > 5 and '处' in i and '罚' in i:
                zwlist = zwlist[id:]
                break
        for i in zwlist:
            i = i.replace('：', ':').strip()
            if len(i) > 1 and len(i) < 45:
                if i.endswith(':'):
                    person = i[:-1]
                elif '单位:' in i:
                    person = i.split(':')[-1]
                elif '个人:' in i:
                    person = i.split(':')[-1]
                elif '被处罚者:' in i:
                    person = i.split(':')[-1]
                elif '处罚对象:' in i:
                    person = i.split(':')[-1]
                elif ContentTypeEntname(i):
                    person = i
                else:
                    person = ''
                if person:
                    if ContentTypePerson(person, (1, 5)):
                        break
                    else:
                        address_, shortname_, type, end1, end2 = companysplit(i)
                        if type or end1:
                            break

    if len(person) > 1 and len(person) < 45:
        return person
    else:
        return None


def get_person_by_title(title):
    if '（' in title:
        person = title.split('（')[1].split('）')[0]
    elif '(' in title:
        person = title.split('(')[1].split(')')[0]
    else:
        person = ''
    return person


def get_wh_by_zwlist(zwlist):
    wh = None
    for z in zwlist:
        if z:
            if ('罚' in z or '环' in z) and '号' in z and 8 < len(z) < len('郑环罚决字〔2019〕031号') + 10:
                wh = z
                break
    return wh
